//
//  CarConfigureVC.m
//  New_Runway
//
//  Created by mack on 2018/1/25.
//  Copyright © 2018年 NelsonJ. All rights reserved.
//

#import "CarConfigureVC.h"
#import "CarConfigHeaferView.h"
#import "CarConfigInfoModel.h"
#import "CarConfigCell.h"
#import "CarBaseInfoModel.h"
#import "CarBaseInfoCell.h"


@interface CarConfigureVC ()<UITableViewDelegate,UITableViewDataSource>
@property(nonatomic, strong) UITableView *tableview;
@property (nonatomic, strong) NSMutableArray *listArray;
@property (nonatomic, strong) NSMutableArray *infoData;


@end

@implementation CarConfigureVC
-(NSMutableArray *)listArray{
    if (!_listArray) {
        _listArray = [[NSMutableArray alloc] init];
    }
    return _listArray;
}
-(NSMutableArray *)infoData{
    
    if (!_infoData) {
        _infoData = [[NSMutableArray alloc] init];
    }
    return _infoData;
}
-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self.tableview reloadData];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationBarView.hidden = YES;

    [self initTableview];
    
    [self carInfo];
    
    
    
}
-(void)carInfo{
    
    
    
    NSArray *arr = @[@{@"childCarConf":@[
                                  
                                  @{
                                      @"confName":@"厂商",
                                      @"infoName": _infomodel.result.model.modelManufacturer ?_infomodel.result.model.modelManufacturer : @"--"
                                      },
                                  @{
                                      @"confName":@"发动机",
                                      @"infoName":_infomodel.result.model.modelEngineModel ?_infomodel.result.model.modelEngineModel : @"--"

                                      },
                                  @{
                                      @"confName":@"变速箱",
                                      @"infoName":_infomodel.result.model.modelGearbox ?_infomodel.result.model.modelGearbox : @"--"

                                      },
                                  @{
                                      @"confName":@"车身结构",
                                      @"infoName":_infomodel.result.model.modelBodywork ?_infomodel.result.model.modelBodywork : @"--"

                                      }
                                  
                                  ],
                          @"confName":@"基本参数",
                          @"selectedState":@"0"
                          },
                     
                      @{@"childCarConf":@[
                                
                                @{
                                    @"confName":@"长度（mm）",
                                    @"infoName":_infomodel.result.model.modelLength ? _infomodel.result.model.modelLength : @"--"
                                    },
                                @{
                                    @"confName":@"宽度（mm）",
                                    @"infoName":_infomodel.result.model. modelWidth ? _infomodel.result.model. modelWidth : @"--"
                                    
                                    },
                                @{
                                    @"confName":@"高度（mm）",
                                    @"infoName":_infomodel.result.model.modelHeight ? _infomodel.result.model.modelHeight : @"--"
                                    
                                    },
                                @{
                                    @"confName":@"轴距（mm）",
                                    @"infoName":_infomodel.result.model.modelWheelbase ? _infomodel.result.model.modelWheelbase : @"--"
                                    
                                    },
                                @{
                                    @"confName":@"车身结构",
                                    @"infoName":_infomodel.result.model.modelBody ? _infomodel.result.model.modelBody : @"--"
                                    
                                    }
                                
                                
                                ],
                        @"confName":@"车身",
                        @"selectedState":@"0"
                        },
                     @{@"childCarConf":@[
                               
                               @{
                                   @"confName":@"发动机型号",
                                   @"infoName":_infomodel.result.model.modelEngineModel ? _infomodel.result.model.modelEngineModel : @"--"
                                   },
                               @{
                                   @"confName":@"排量（ml）",
                                   @"infoName":_infomodel.result.model.modelDisplacement ? _infomodel.result.model.modelDisplacement : @"--"
                                   
                                   },
                               @{
                                   @"confName":@"进气形式",
                                   @"infoName":_infomodel.result.model.modelInletModel ? _infomodel.result.model.modelInletModel : @"--"
                                   
                                   },
                               @{
                                   @"confName":@"气缸排列形式",
                                   @"infoName":_infomodel.result.model.modelCylinderArray ? _infomodel.result.model.modelCylinderArray : @"--"
                                   
                                   },
                               @{
                                   @"confName":@"气缸数（个）",
                                   @"infoName":_infomodel.result.model.modelCylinderNum ? _infomodel.result.model.modelCylinderNum : @"--"
                                   
                                   },
                               @{
                                   @"confName":@"燃料形式",
                                   @"infoName":_infomodel.result.model.modelFuelRoz ? _infomodel.result.model.modelFuelRoz :@"--"
                                   
                                   },
                               @{
                                   @"confName":@"环保标准",
                                   @"infoName":_infomodel.result.model.modelEnviron ? _infomodel.result.model.modelEnviron : @"--"
                                   
                                   }
                               
                               ],
                       @"confName":@"发动机",
                       @"selectedState":@"0"
                       },
                     @{@"childCarConf":@[
                               
                               @{
                                   @"confName":@"变速箱类型",
                                   @"infoName":_infomodel.result.model.modelGearbox ? _infomodel.result.model.modelGearbox : @"--"
                                   },
                               @{
                                   @"confName":@"档位个数",
                                   @"infoName":_infomodel.result.model.modelGearNum ? _infomodel.result.model.modelGearNum : @"--"
                                   
                                   }
                              
                               
                               ],
                       @"confName":@"变速箱",
                       @"selectedState":@"0"
                       },
                     
                     ];
    for (NSDictionary *dic in arr) {
        CarConfigResult *child = [CarConfigResult mj_objectWithKeyValues:dic];
        [self.listArray addObject:child];

    }
    [self loadCarConfigInfo];


    
}
-(void)initTableview{
    kSelfWeak;
    self.tableview = [[UITableView alloc] init];
    self.tableview.delegate = self;
    self.tableview.dataSource = self;
    self.tableview.backgroundColor = MAIN_COLOR();
    self.tableview.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.tableview registerClass:[CarConfigHeaferView class] forHeaderFooterViewReuseIdentifier:@"header"];
    [self.tableview registerClass:[CarConfigCell class] forCellReuseIdentifier:@"CarConfigCell"];
    [self.tableview registerClass:[CarBaseInfoCell class] forCellReuseIdentifier:@"CarBaseInfoCell"];
//    [self.tableview registerClass:[UITableViewCell class] forCellReuseIdentifier:@"CELL"];
    self.tableview.estimatedRowHeight = 0;
    self.tableview.estimatedSectionHeaderHeight = 0;
    self.tableview.estimatedSectionFooterHeight = 0;
    [self.view addSubview:self.tableview];
    [self.tableview mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.bottom.equalTo(weakSelf.view);
    }];
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return self.listArray.count ;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    
   
    CarConfigResult *info = self.listArray[section];
    
    if (info.selectedState == NO) {
        return 0;
    }else{
        
        return info.childCarConf.count;
    }
  
    
   
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 41;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    CarConfigResult *info = self.listArray[indexPath.section];
    Childcarconf *childInfo = info.childCarConf[indexPath.row];
    if (indexPath.section < 4) {
          CarBaseInfoCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CarBaseInfoCell"];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.leftLable.text = childInfo.confName;
        cell.rightLable.text = childInfo.infoName;
        return cell;
    }
    
    CarConfigCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CarConfigCell"];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.label.text = childInfo.confName;
    if (childInfo.selectedState == YES) {
        cell.leftImage.image = [UIImage imageNamed:@"hook"];
    }else{
        
        cell.leftImage.image = [UIImage imageNamed:@"circle"];

    }
    return cell;
}


- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    CarConfigResult *info = self.listArray[section];
    
    CarConfigHeaferView *view = [tableView dequeueReusableHeaderFooterViewWithIdentifier:@"header"];
    view.label.text = info.confName;
    view.tag = section;
    if (info.selectedState == YES) {
        view.rightImage.image = [UIImage imageNamed:@"up"];

    }else{
        view.rightImage.image = [UIImage imageNamed:@"down"];
    }
    
    
    if (view.gestureRecognizers == nil) {
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(headerViewClickedAction:)];
        [view addGestureRecognizer:tap];
    }
    
    return view;
    


}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}
- (void) headerViewClickedAction:(UITapGestureRecognizer *)sender
{
    
    CarConfigResult *info = self.listArray[sender.view.tag];
    
    
    if (info.selectedState == YES) {
        info.selectedState = NO;
        
    }else{
        info.selectedState = YES;

    }

    NSIndexSet *set = [NSIndexSet indexSetWithIndex:sender.view.tag];
    [self.tableview reloadSections:set withRowAnimation:UITableViewRowAnimationFade];
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    
    return 41;
}
-(void)loadCarConfigInfo{
    
    [SVProgressHUD show];
    [YQNetworking postWithUrl:[NSString stringWithFormat:@"%@%@",Api,carConfApi] refreshRequest:YES cache:NO params:@{@"carId":self.carID,@"modelId":@(self.modelID)} progressBlock:^(int64_t bytesRead, int64_t totalBytes) {
        
    } successBlock:^(id response) {
        CarConfigInfoModel *configInfo = [CarConfigInfoModel mj_objectWithKeyValues:response];

        for (NSDictionary *dic in configInfo.result) {
            CarConfigResult *child = [CarConfigResult mj_objectWithKeyValues:dic];
            [self.listArray addObject:child];

        }
        
        [self.tableview reloadData];
        [SVProgressHUD dismiss];

    } failBlock:^(NSError *error) {
        [ShowMessage showHintMessage:@"请求失败!" duration:2.0 withRemindId:remindFail];

        [SVProgressHUD dismiss];

    }];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



@end
